package com.css.fxfzfxqh.modules.ysx.service.impl;

import com.css.fxfzfxqh.modules.ysx.entity.FxqhJzwYSX;
import com.css.fxfzfxqh.modules.ysx.entity.FxqhJzwYSXSub;
import com.css.fxfzfxqh.modules.ysx.param.FxqhJzwYSXParam;
import com.css.fxfzfxqh.modules.ysx.param.MatchingParam;
import com.css.fxfzfxqh.modules.ysx.service.JzwYSXRepository;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/07/21
 * @Description: 易损性临时
 */
@Repository
public class JzwYSXRepositoryImpl implements JzwYSXRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    //易损性分页查询,正式版
    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, FxqhJzwYSXParam param) {
        StringBuilder sql = new StringBuilder("select id, province, city, county, structure_type1 as structureType, structure_type2 as structureType1, fortify as fortification, year as years," +
                " housing_use1 as use0, housing_use2 as use1, number_of_floors as numberOfFloors, ysx_type as ysxType, create_name as createName, create_time as createTime from fxqh_jzwysx ");
        StringBuilder whereSql = new StringBuilder(" where 1 = 1 and role_type='" + param.getRoleType() + "' and ysx_type='" + param.getYsxType() + "' ");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        //省市县
        if (!PlatformObjectUtils.isEmpty(param.getProvince())) {
            whereSql.append(" and province = '" + param.getProvince() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getCity())) {
            whereSql.append(" and city = '" + param.getCity() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getCounty())) {
            whereSql.append(" and county = '" + param.getCounty() + "'");
        }
        //结构类型
        if (!PlatformObjectUtils.isEmpty(param.getStructureType1())) {
            whereSql.append(" and structure_type1 = '" + param.getStructureType1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getStructureType2())) {
            whereSql.append(" and structure_type2 = '" + param.getStructureType2() + "'");
        }
        //设防情况
        if (!PlatformObjectUtils.isEmpty(param.getFortify())) {
            whereSql.append(" and fortify = '" + param.getFortify() + "'");
        }
        //年代
        if (!PlatformObjectUtils.isEmpty(param.getYear())) {
            whereSql.append(" and year = '" + param.getYear() + "'");
        }
        //房屋用途
        if (!PlatformObjectUtils.isEmpty(param.getHousingUse1())) {
            whereSql.append(" and housing_use1 = '" + param.getHousingUse1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getHousingUse2())) {
            whereSql.append(" and housing_use2 = '" + param.getHousingUse2() + "'");
        }
        //层数
        if (!PlatformObjectUtils.isEmpty(param.getNumberOfFloors())) {
            whereSql.append(" and number_of_floors = '" + param.getNumberOfFloors() + "'");
        }
        //维护人
        if (!PlatformObjectUtils.isEmpty(param.getCreateName())) {
            whereSql.append(" and create_name like '%" + param.getCreateName() + "%'");
        }
        //维护开始时间
        if (!PlatformObjectUtils.isEmpty(param.getStartTime())) {
            whereSql.append(" and to_char(create_time, 'YYYY-MM-DD') >= '" + param.getStartTime() + "'");
        }
        //维护结束时间
        if (!PlatformObjectUtils.isEmpty(param.getEndTime())) {
            whereSql.append(" and to_char(create_time, 'YYYY-MM-DD') <= '" + param.getEndTime() + "'");
        }
        sql.append(whereSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(FxqhJzwYSX.class)).size();
        List<FxqhJzwYSX> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(FxqhJzwYSX.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public boolean saveYsx(FxqhJzwYSXParam fxqhJzwYSX) {
        String fxqhSql = "insert into fxqh_jzwysx (id, province, city, county, structure_type1,structure_type2," +
                " fortify,year, housing_use1,housing_use2,number_of_floors, " +
                "ysx_type,create_user,create_name,create_time,role_type) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(fxqhSql, fxqhJzwYSX.getId(), fxqhJzwYSX.getProvince(), fxqhJzwYSX.getCity(), fxqhJzwYSX.getCounty(), fxqhJzwYSX.getStructureType1(), fxqhJzwYSX.getStructureType2(),
                fxqhJzwYSX.getFortify(), fxqhJzwYSX.getYear(), fxqhJzwYSX.getHousingUse1(), fxqhJzwYSX.getHousingUse2(), fxqhJzwYSX.getNumberOfFloors(), fxqhJzwYSX.getYsxType(),
                fxqhJzwYSX.getCreateUser(), fxqhJzwYSX.getCreateName(), fxqhJzwYSX.getCreateTime(), fxqhJzwYSX.getRoleType()) > 0;
    }

    @Override
    public void delete(FxqhJzwYSXParam fxqhJzwYSX) {
        StringBuilder sql = new StringBuilder("select id from fxqh_jzwysx ");
        StringBuilder whereSql = new StringBuilder(" where 1 = 1  and ysx_type='" + fxqhJzwYSX.getYsxType() + "' and role_type='" + fxqhJzwYSX.getRoleType() + "' ");
        //省市县
        if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getProvince())) {
            whereSql.append(" and province = '" + fxqhJzwYSX.getProvince() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getCity())) {
            whereSql.append(" and city = '" + fxqhJzwYSX.getCity() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getCounty())) {
            whereSql.append(" and county = '" + fxqhJzwYSX.getCounty() + "'");
        }
        //结构类型
        if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getStructureType1())) {
            whereSql.append(" and structure_type1 = '" + fxqhJzwYSX.getStructureType1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getStructureType2())) {
            whereSql.append(" and structure_type2 = '" + fxqhJzwYSX.getStructureType2() + "'");
        }
        //设防情况
        if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getFortify())) {
            whereSql.append(" and fortify = '" + fxqhJzwYSX.getFortify() + "'");
        }
        if ("1".equals(fxqhJzwYSX.getYsxType())) {
            //年代
            if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getYear())) {
                whereSql.append(" and year = '" + fxqhJzwYSX.getYear() + "'");
            }
            //房屋用途
            if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getHousingUse1())) {
                whereSql.append(" and housing_use1 = '" + fxqhJzwYSX.getHousingUse1() + "'");
            }
            if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getHousingUse2())) {
                whereSql.append(" and housing_use2 = '" + fxqhJzwYSX.getHousingUse2() + "'");
            }
            //层数
            if (!PlatformObjectUtils.isEmpty(fxqhJzwYSX.getNumberOfFloors())) {
                whereSql.append(" and number_of_floors = '" + fxqhJzwYSX.getNumberOfFloors() + "'");
            }
        }
        sql.append(whereSql);
        List<FxqhJzwYSX> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(FxqhJzwYSX.class));
        if (list != null && list.size() > 0) {
            for (FxqhJzwYSX jzwYSX : list) {
                String sql1 = "delete from fxqh_jzwysx  where id='" + jzwYSX.getId() + "'";
                jdbcTemplate.update(sql1);
                String sql2 = "delete from fxqh_jzwysx_sub  where pid='" + jzwYSX.getId() + "'";
                if ("1".equals(fxqhJzwYSX.getYsxType()) || "2".equals(fxqhJzwYSX.getYsxType())) {
                    sql2 += "and intensity='" + fxqhJzwYSX.getDamageDegree() + "'";
                }
                jdbcTemplate.update(sql2);
            }
        }
    }

    @Override
    public int delete(String province, String city, String county, String ysxType) {
        String sql1 = "delete from fxqh_jzwysx  where province = '" + province + "' and city = '" + city + "'" +
                " and county = '" + county + "' and ysx_type='" + ysxType + "'";
        return jdbcTemplate.update(sql1);
    }

    @Override
    public void saveYsxSub(FxqhJzwYSXSub jzwYSXSub) {
        String fxqhSql = "insert into fxqh_jzwysx_sub (pid,intensity,damage,rate ) values (?,?,?,?)";
        jdbcTemplate.update(fxqhSql, jzwYSXSub.getPid(), jzwYSXSub.getIntensity(), jzwYSXSub.getDamage(), jzwYSXSub.getRate());
    }

    @Override
    public List<FxqhJzwYSXSub> getPageSubById(String pid) {
        StringBuilder sql = new StringBuilder("select * from fxqh_jzwysx_sub where pid = '" + pid + "'");
        List<FxqhJzwYSXSub> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(FxqhJzwYSXSub.class));
        return list;
    }

    @Override
    public List<String> getSfIntensity(FxqhJzwYSXParam param) {
        StringBuilder sql = new StringBuilder("select distinct(fortify) from fxqh_jzwysx ");
        StringBuilder whereSql = new StringBuilder(" where 1 = 1 and role_type='" + param.getRoleType() + "' and ysx_type='" + param.getYsxType() + "' ");
        //省市县
        if (!PlatformObjectUtils.isEmpty(param.getProvince())) {
            whereSql.append(" and province = '").append(param.getProvince()).append("'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getCity())) {
            whereSql.append(" and city = '").append(param.getCity()).append("'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getCounty())) {
            whereSql.append(" and county = '").append(param.getCounty()).append("'");
        }
        //结构类型
        if (!PlatformObjectUtils.isEmpty(param.getStructureType1())) {
            whereSql.append(" and structure_type1 = '" + param.getStructureType1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getStructureType2())) {
            whereSql.append(" and structure_type2 = '" + param.getStructureType2() + "'");
        }
        sql.append(whereSql);
        List<String> list = jdbcTemplate.queryForList(sql.toString(), String.class);
        return list;
    }

    @Override
    public void update(FxqhJzwYSXSub entity) {
        jdbcTemplate.update("update fxqh_jzwysx_sub set rate='"+entity.getRate()+"' where pid='"+entity.getPid()+"' and intensity='"+entity.getIntensity()+"' and damage='"+entity.getDamage()+"'");
    }
}
